package com.meilingcloud.web.controller.tool;

import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.system.domain.SysLabelTemp;
import com.meilingcloud.system.service.ISysLabelTempService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 标签模板 Controller
 *
 * @author chenl
 * @date 2021-04-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/tool/labelTemp")
public class SysLabelTempController extends BaseController {

    private final ISysLabelTempService iSysLabelTempService;

    /**
     * 查询标签模板 列表
     */
    @PreAuthorize("@ss.hasPermi('tool:labelTemp:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLabelTemp sysLabelTemp) {
        startPage();
        List<SysLabelTemp> list = iSysLabelTempService.queryList(sysLabelTemp);
        iSysLabelTempService.handleResult(list);
        return getDataTable(list);
    }

    /**
     * 获取启用的标签模板
     */
    @GetMapping(value = "/getActive")
    public AjaxResult getActiveTemp() {
        return AjaxResult.success(iSysLabelTempService.getActiveTemp());
    }

    /**
     * 获取标签模板 详细信息
     */
    @PreAuthorize("@ss.hasPermi('tool:labelTemp:query')")
    @GetMapping(value = "/{labelTempId}")
    public AjaxResult getInfo(@PathVariable("labelTempId") Long labelTempId) {
        return AjaxResult.success(iSysLabelTempService.getTempById(labelTempId));
    }

    /**
     * 新增标签模板
     */
    @PreAuthorize("@ss.hasPermi('tool:labelTemp:add')")
    @Log(title = "标签模板 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLabelTemp sysLabelTemp) {
        sysLabelTemp.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSysLabelTempService.saveTemp(sysLabelTemp));
    }

    /**
     * 修改标签模板
     */
    @PreAuthorize("@ss.hasPermi('tool:labelTemp:edit')")
    @Log(title = "标签模板 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLabelTemp sysLabelTemp) {
        sysLabelTemp.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSysLabelTempService.updateTemp(sysLabelTemp));
    }

    /**
     * 删除标签模板
     */
    @PreAuthorize("@ss.hasPermi('tool:labelTemp:remove')")
    @Log(title = "标签模板 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{labelTempIds}")
    public AjaxResult remove(@PathVariable Long[] labelTempIds) {
        return toAjax(iSysLabelTempService.removeTemps(Arrays.asList(labelTempIds)));
    }
}
